package com.zhang.jedis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhangchenghui
 * @version 1.0.0
 * @ClassName MyJedisCluster.java
 * @createTime 2021年09月06日 15:27:00
 * @Description 集群用法
 */
public class MyJedisCluster {
    private static JedisCluster jedisCluster;

    public static JedisCluster getJedisCluster (){
        if(jedisCluster == null) {

            //创建一个连接池配置
            JedisPoolConfig poolConfig = new JedisPoolConfig();

            poolConfig.setMinIdle(2);
            poolConfig.setMaxIdle(5);
            poolConfig.setMaxTotal(10);
            poolConfig.setBlockWhenExhausted(true);
            poolConfig.setMaxWaitMillis(2000);

            poolConfig.setTestOnBorrow(true);

            // 集群机器集合
            Set<HostAndPort> nodes = new HashSet<>();
            nodes.add(new HostAndPort("192.168.75.102", 6379));
            nodes.add(new HostAndPort("192.168.75.103", 6379));
            nodes.add(new HostAndPort("192.168.75.104", 6379));
            nodes.add(new HostAndPort("192.168.75.102", 6380));
            nodes.add(new HostAndPort("192.168.75.103", 6380));
            nodes.add(new HostAndPort("192.168.75.104", 6380));

            jedisCluster = new JedisCluster(nodes,poolConfig);
        }
        return jedisCluster;
    }
}
